/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import javax.persistence.CascadeType;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;

/**
 *
 * @author UniceAm
 */

@Entity
@NamedQueries({
    @NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c ORDER BY c.id ASC"),
    @NamedQuery(name = "Client.count", query = "SELECT count(c) FROM Client c"),
    @NamedQuery(name = "Client.findById", query = "SELECT c FROM Client c WHERE c.id = :id ORDER BY c.id DESC"),
    @NamedQuery(name = "Client.findByPrenom", query = "SELECT c FROM Client c WHERE c.prenom = :prenom"),
    @NamedQuery(name = "Client.findByAdresse", query = "SELECT c FROM Client c WHERE c.adresse = :adresse"),
    @NamedQuery(name = "Client.findByLogin", query = "SELECT c FROM Client c WHERE c.login = :login"),
    @NamedQuery(name = "Client.findByPassword", query = "SELECT c FROM Client c WHERE c.password = :password"),
    @NamedQuery(name = "Client.findByNom", query = "SELECT c FROM Client c WHERE c.nom = :nom")})

public class Client implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String nom;
    private String prenom;
    private String adresse;
    private String login;
    private String password;
    // 1 client est associe a 1 compte courant
    
    //@OneToOne(cascade = CascadeType.ALL)
    
    @OneToOne(cascade = {CascadeType.ALL, CascadeType.REMOVE})
    private CompteCourant compteCourant;
    
    
    
    // 1 client peut posseder plusieurs comptes epargne
    @OneToMany(mappedBy = "client")
    private Collection<CompteEpargne> listeCompteEpargne;
    
    
    
    //@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    //private Collection<CompteEpargne> listeCompteEpargne;
    
    
    // plusieurs clients peuvent etre associes a un compte joint
    @ManyToOne(cascade = CascadeType.ALL)
    private CompteJoint compteJoint;

    public Client() {
    }

    public Client(String nom, String prenom, String adresse, String login, String password) {
        this.nom = nom;
        this.prenom = prenom;
        this.adresse = adresse;
        this.login = login;
        this.password = password;
        listeCompteEpargne = new ArrayList<CompteEpargne>();
    }

    public Client(String nom, String prenom, String adresse, String login, String password, CompteCourant compteCourant) {
        this.nom = nom;
        this.prenom = prenom;
        this.adresse = adresse;
        this.login = login;
        this.password = password;
        this.compteCourant = compteCourant;
        listeCompteEpargne = new ArrayList<CompteEpargne>();

    }

    public CompteCourant getCompteCourant() {
        return compteCourant;
    }

    public void setCompteCourant(CompteCourant compteCourant) {
        this.compteCourant = compteCourant;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public void setCompteJoint(CompteJoint compteJoint) {
        this.compteJoint = compteJoint;
    }

    public Collection<CompteEpargne> getListeCompteEpargne() {
        return listeCompteEpargne;
    }

    public void setListeCompteEpargne(Collection<CompteEpargne> listeCompteEpargne) {
        this.listeCompteEpargne = listeCompteEpargne;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Client)) {
            return false;
        }
        Client other = (Client) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Client[ id=" + id + " ]";
    }
}